//
//  CZTabBar.swift
//  weibo24
//
//  Created by 李文峰 on 16/2/25.
//  Copyright © 2016年 fengten. All rights reserved.
//

import UIKit

class CZTabBar: UITabBar {
    
    //UIVie的指定构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        //子控件的排布方式
        setupU()
    }
    //系统错误提示代码
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupU(){
        
        
        addSubview(publishButton)
        
        //只有一个UIButton
        //        printLog(subviews)
    }
    
    
    //控件frame的排布
    override func layoutSubviews() {
        //调用父类
        super.layoutSubviews()
        //tabbarItem的frame
        //索引
        var index = 0
        //宽度
        let width = bounds.width / 5
        //遍历子控件 并判定是tabbarButtin
        for subview in subviews{
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!)
                //            if subview.isKindOfClass(NSClassFromString("UITabBarBtton")!)
            {
                subview.frame = CGRectMake(width * CGFloat(index++), bounds.origin.y, width, bounds.size.height)
                
                //空出中间位置
                if index == 2{index++}
            }
        }
        //设置发布按钮的frame
        publishButton.frame = CGRectMake(width * 2, bounds.origin.y, width, bounds.size.height)
    }
    
    //懒加载
    lazy var publishButton: UIButton = {
        
        //实例化
        let button = UIButton(type: .Custom)
        //设置属性
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Selected)
        
        //设置大小
        button.sizeToFit()
        
        return button
        }()
}
